﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.Model;
using OgilvyOne.BLL;

public partial class admin_manager_userAdd : AdminBasePage
{
    string user;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.PopedomValidate(Layerid, ADD);
        user = new AdminBasePage().GetSessionUsersModel().Username;
        if (!IsPostBack)
        {
            header.Text = new ColumnBll().LayeridSysColumnNaiv(Layerid);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string username = txtUSERNAME.Value;
        string pwd = txtUSERPASS.Value;
        string pwdse = UserPwdse.Value;
        if (pwd == null || pwd == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), Guid.NewGuid().ToString(), "alert('用户名不能为空！')", true);
            return;
        }
        if (username == null || username == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), Guid.NewGuid().ToString(), "alert('用户密码不能为空！')", true);
            return;
        }
        if (pwdse != null && pwdse == pwd)
        {
            UsersBll bll = new UsersBll();
            if (!bll.Exists(username,0))
            {
                UsersModel model = new UsersModel();
                model.Addtime = DateTime.Now;
                model.Username = username;
                model.Userpass = MD5.GetMD5(pwd);
                if (new UsersBll().Add(model) > 0)
                {
                    this.insertLog(user + "添加用户: " + username);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), Guid.NewGuid().ToString(), "alert('保存成功！');window.location=\"user_list.aspx?layerid=" + Layerid + "\"", true);
                }
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), Guid.NewGuid().ToString(), "alert('用户名已存在！');", true);
            }

        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), Guid.NewGuid().ToString(), "alert(" + "\"两次输入的密码不同！\"" + ")", true);
            return;
        }
    }
}
